package com.saurshaz.quickfixj.engine;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import quickfix.Application;
import quickfix.ConfigError;
import quickfix.DefaultMessageFactory;
import quickfix.DoNotSend;
import quickfix.FieldConvertError;
import quickfix.FieldNotFound;
import quickfix.Group;
import quickfix.IncorrectDataFormat;
import quickfix.IncorrectTagValue;
import quickfix.LogFactory;
import quickfix.MemoryStoreFactory;
import quickfix.Message;
import quickfix.MessageCracker;
import quickfix.MessageFactory;
import quickfix.MessageStoreFactory;
import quickfix.RejectLogon;
import quickfix.ScreenLogFactory;
import quickfix.SessionID;
import quickfix.SessionSettings;
import quickfix.SocketAcceptor;
import quickfix.SocketInitiator;
import quickfix.UnsupportedMessageType;
import quickfix.field.Account;
import quickfix.field.CollInquiryID;
import quickfix.field.Quantity;
import quickfix.fix44.CollateralReport;
import quickfix.fix44.TradingSessionStatus;

public class Acceptor extends MessageCracker implements Application {
	private CollInquiryID mColInquiryID;
	private CollateralReport mCollateralReport;
	private String mMsgReqID;
	private long mMsgSent;
	private boolean mOpenRange;
	private boolean mOTO;
	private String mPassword;
	private String mPIN;
	private boolean mPreviouslyQuoted;
	private boolean mPrintMDS;
	private long mRequestID;
	private SessionID mSessionID;
	private TradingSessionStatus mSessionStatus;
	private Date mStartSession;
	private String mUsername;
	private Calendar mUTCCal = Calendar
			.getInstance(TimeZone.getTimeZone("UTC"));

	private Acceptor(String aUsername, String aPassword, String aPIN) {
		mUsername = aUsername;
		mPassword = aPassword;
		mPIN = aPIN;
		mPrintMDS = false;

		mCollateralReport = new CollateralReport();
		mCollateralReport.set(new Account("ACC"));
		mCollateralReport.set(new CollInquiryID("CEId"));
		mCollateralReport.set(new Quantity(10000));
		mCollateralReport.setGroups(new Group(1, 0));
	}

	public static void main(String[] args) {
		String config = System.getProperty("user.dir")+"/acceptor.cfg";
		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(config);
			SessionSettings settings;
			settings = new SessionSettings(fileInputStream);
			String username = settings.getString("username");
			String password = settings.getString("password");
			String pin = settings.getDefaultProperties().getProperty("pin",
					null);

			Acceptor app = new Acceptor(username, password, pin);
			MessageStoreFactory storeFactory = new MemoryStoreFactory();
			LogFactory logFactory = new ScreenLogFactory();
			MessageFactory messageFactory = new DefaultMessageFactory();
			SocketAcceptor acceptor = new SocketAcceptor(app, storeFactory,
					settings, logFactory, messageFactory);
			acceptor.start();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));

		} catch (ConfigError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FieldConvertError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onCreate(SessionID sessionId) {
		System.out.println("onCreate");
	}

	@Override
	public void onLogon(SessionID sessionId) {
		System.out.println("onLogon");
	}

	@Override
	public void onLogout(SessionID sessionId) {
		System.out.println("onLogout");
	}

	@Override
	public void toAdmin(Message message, SessionID sessionId) {
		System.out.println("toAdmin");
	}

	@Override
	public void fromAdmin(Message message, SessionID sessionId)
			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
			RejectLogon {
		System.out.println("fromAdmin");
	}

	@Override
	public void toApp(Message message, SessionID sessionId) throws DoNotSend {
		System.out.println("toApp");
	}

	@Override
	public void fromApp(Message message, SessionID sessionId)
			throws FieldNotFound, IncorrectDataFormat, IncorrectTagValue,
			UnsupportedMessageType {
		System.out.println("fromApp");
	}
}
